<!--
 * @Author: [you name]
 * @Date: 2021-09-17 09:16:36
 * @LastEditors: [you name]
 * @LastEditTime: 2021-09-23 17:06:04
 * @Description: 
-->
<template>
  <div>
    <Header></Header>
    <!-- 体部 -->
    <div class="content">
      <div class="wrapper">
        <!-- 标题 -->
        <div class="title">{{ category.name }}</div>
        <!-- 文章列表区域 -->
        <div class="artListArea" v-if="artData.total > 0">
          <!-- 文章列表 -->
          <div
            class="artList"
            @click="toArticle(artItem)" v-for="artItem in artData.list"
            :key="artItem.id"
          >
            <!-- logo -->
            <div class="logo">
              <img :src="artItem.cover" alt="" />
            </div>
            <!-- 文章详细信息 -->
            <div class="detailMsg">
              <!-- 文章标题 -->
              <div class="title">{{ artItem.title }}</div>
              <!-- 发布时间 -->
              <div class="puTime">
                发布时间：{{ artItem.publishTime | fmtDate }}
              </div>
              <!-- 阅读次数 -->
              <div class="readTime">阅读次数：{{ artItem.readTimes }}</div>
            </div>
          </div>
          <!-- 分页 -->
          <div class="page">
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page.sync="param.page"
              :page-size="param.pageSize"
              layout="total, prev, pager, next"
              :total="artData.total"
            >
            </el-pagination>
          </div>
        </div>
        <div class="noData" style="" v-else>暂无数据</div>
      </div>
    </div>
  </div>
</template>

<script>
// 引入get请求
import { get } from "../utils/request";
import Header from "./components/Header.vue";
export default {
  components: {
    Header,
  },
  data() {
    return {
      category: {},
      // 分页参数
      param: {
        page: 1,
        pageSize: 4,
      },

      // 文章列表数据
      artData: {},
    };
  },
  methods: {
    // 分页查询文章列表数据
    getArticleData() {
      this.param.categoryId = this.category.id;
      // console.log(this.param);
      get("/index/article/pageQuery", this.param).then((res) => {
        this.artData = res.data;
      });
    },
    // 分页方法
    handleCurrentChange(val) {
      // 使用当前页数与我们的分页控制的参数进行赋值
      this.param.page = val;
      this.getArticleData();
    },
    // 跳转到文章页面
    toArticle(artItem){
      this.$router.push({
        path:'/article',
        query:artItem
      })
    }
  },
  // 侦听器
  watch: {
    "$route.query": {
      handler(query) {
        // alert(JSON.stringify(query));
        this.category = query;
        // 每次我们点击菜单栏的时候 将page重置为第一页
        this.param.page = 1;
        this.getArticleData();
      },
    },
    // 开启深度监听
    deep: true,
  },
  created() {
    // 将路由传递过来的参数进行赋值
    this.category = this.$route.query;
    // 默认加载文章列表数据
    this.getArticleData();
  },
};
</script>

<style lang='scss' scoped>
.content {
  background-color: #ecf6f2;
  min-height: 500px;
  padding: 10px;
  .wrapper {
    .title {
      color: #528c38;
      font-size: 20px;
    }
    .artListArea {
      background-color: white;
      height: 300px;
      .artList {
        padding: 10px;
        border-bottom: 1px solid gainsboro;
        display: flex;
        cursor: pointer;
        .logo {
          flex: 1;
          img {
            width: 200px;
          }
        }
        .detailMsg {
          flex: 2;
          line-height: 3em;
          .title {
            color: black;
          }
        }
      }
      .page {
        text-align: center;
        padding: 1em;
      }
    }
    .noData{
      background-color: white;
      height: 200px;
      text-align: center;
      font-size: 30px;
      color: red;
    }
  }
}
</style>